# -*- coding: utf-8 -*-
import simplejson


def get_evaluator(sname):
  if sname == 'verbs_simple_present_sense_conjugation':
    return verbs_simple_present_sense_conjugation

VOWELS = [u'a',u'e',u'ı',u'i',u'o',u'ö',u'u',u'ü']
def smallharmony(v):
  if v in [u'a',u'ı',u'o',u'u']:
    return 'a'
  elif v in [u'e',u'i',u'ö',u'ü']:
    return u'e'
  else:
    assert False, "Not a vowel"

def largeharmony(v):
  if v in [u'e',u'i']: return u'i';
  elif v in [u'a',u'ı']: return u'ı';
  elif v in [u'o',u'ö']: return u'ö';
  elif v in [u'u',u'ü']: return u'ü';
  else: assert False, "Not a vowel"

def yorharmony(v):
  # different from the rest
  if v in [u'e',u'i']: return u'i';
  elif v in [u'a',u'ı']: return u'ı';
  elif v in [u'o',u'u']: return u'u';
  elif v in [u'ö',u'ü']: return u'ü';
  else: assert False, "Not a vowel"
  


def lastvowel(base):
  i = len(base)-1
  while i>0 and base[i] not in VOWELS: i -= 1
  assert base[i] in VOWELS
  return base[i]

GRAM_PERS_LIST = ['s1', 's2', 's3', 'p1', 'p2', 'p3']
GRAM_PERS = {
  's1':'1st person singular', 
  's2':'2nd person singular', 
  's3':'3rd person singular', 
  'p1':'1st person plural', 
  'p2':'2nd person plural', 
  'p3':'3rd person plural', 
}
VERB_SUFFICES = {
  's1':u'ım',
  's2':u'sın',
  's3':u'',
  'p1':u'ız',
  'p2':u'sınız',
  'p3':u'lar',
}

CHANGING_T = [u'affetmek', u'etmek', u'gitmek', u'tatmak']

def verbsuffix(grampers,vowel):
  return VERB_SUFFICES[grampers].replace(u'ı',largeharmony(vowel))

class Verb:
  def __init__(self, infinitive):
    self.infinitive = infinitive.strip()
    assert self.infinitive[-3:] in [u'mek',u'mak'], "Not correct infinitive"
    assert len(infinitive) >= 4, 'No base'
    self.base = self.infinitive[:-3]
    self.last = self.base[-1] # last letter of base
    self.eiv = self.last in VOWELS # ends in vowel
    self.lv = lastvowel(self.base)
    self.lvsh = smallharmony(self.lv) #last vowel small harmonized
    self.lvlh = largeharmony(self.lv) #last vowel small harmonized
    self.lvyh = yorharmony(self.lv) #last vowel yor harmonized
    self.changingt = self.infinitive in CHANGING_T # if the base ends in a t, and the word is among the irregular group 
  def conj(self, tense, grampers):
    if tense == 'present':
      
      if self.changingt:
        base_expl = 'It is one of the few irregular cases, where the t, which is the last letter of the base, changes to a d'
        base = self.base[:-1]+'d'
      elif not self.eiv:
        base_expl = 'It ends in a consonant, so nothing changes'
        base = self.base
      else:
        base_expl = 'It ends in a vowel, %s,and that vowel must be changed according to the the special vowel harmony only applying to \'yor\' %s' % (self.last, self.lvyh)
        base = (self.base[:-1]+self.lvyh) if self.eiv else self.base
      
      if self.eiv:
        buf = ''
        buf_expl = 'Because the base ends in a vowel, there is no buffer.'
      else:
        buf = self.lvyh
        buf_expl = 'Because the base ends in a consonant we must add a buffer vowel decided by the special vowel harmony only applying to \'yor\' there is no buffer.'
      buf = self.lvyh if not self.eiv else ''
      
      
      yor = 'yor'
      yor_expl = 'this signifies present tense'
      
      personal_suffix = VERB_SUFFICES[grampers].replace(u'ı',u'u')
      personal_suffix_expl = 'Add the correct suffix for the grammatical person. In this case %s' % GRAM_PERS[grampers]

      
      parts = [ 
        ('base',base, base_expl),
        ('buf',buf, buf_expl),
        ('yor',yor, yor_expl),
        ('personal_suffix', personal_suffix, personal_suffix_expl),
      ]
      
      return {
        'correct':base+buf+yor+personal_suffix,
        'parts':parts
      }
      
    elif tense == 'past':
      pass
    else:
      assert False, "Unknown tense"
  def evaluate(self, tense, grampers, s):
    #assert(type(s) == unicode)
    #assert(type(s) == unicode)
    response = self.conj(tense, grampers)
    assert(type(response['correct']) == unicode)
    if response['correct'] == s:
      response['code'] = 0
      response['msg'] = 'correct'
    else:
      response['code'] = 1
      response['msg'] = 'Not correct, try again with this hint. '
    return response
  def __str__(self):
    return self.infinitive
  def pp(self): # prettyprint
    width = 20
    attrs = ['infinitive', 'base', 'last', 'eiv', 'lv', 'lvsh','lvlh']
    l = ['%-20s %s' % (attr,getattr(self,attr)) for attr in attrs]
    return '\n'.join(l)
    
TEST_VERBS = ['yapmak', 'gelmek', 'okumak', 'aramak', 'içmek']

if __name__ == '__main__':

  for infinitive in TEST_VERBS:
    v = Verb(infinitive)
    print v.pp()
    for grampers in GRAM_PERS_LIST:
      print '\t',v.conj('present',grampers)
    print


Q = {
  'lesson_sname':'lesson_02',
  'drill_sname':'lesson_02',
  
  'evaluator' : 'verbs_simple_present_sense_conjugation',
  'tense' : 'present',
  'grampers' : 's2',
  'verb' : 'gelmek',
  'question_text' : "Oğuz evine ___________. Insert the correct conjugation of 'gelmek'.",

  'inputs':[
      {
        'type':'text',
        'name':'Conjugated',
        'id':'answer_text',
      },
  ]
}

class GenEval:
  pass

#verbs_simple_present_sense_conjugation
class VerbsSimplePresentTenseConjugation(GenEval):
  def generate():
    q = {
      'lesson_sname':'lesson_02',
      'drill_sname':'drill_02',
      'evaluator' : 'verbs_simple_present_sense_conjugation',
      'tense' : 'present',
      'grampers' : 's2',
      'verb' : 'gelmek',
      'question_text' : "Oğuz evine ___________. Insert the correct conjugation of 'gelmek'.",

      'inputs':[
          {
            'type':'text',
            'name':'Conjugated',
            'id':'answer_text',
          },
      ]
    }
    return q
  def eval(q):
    verb = Verb(q['gelmek'])
    tense = q['tense']
    grampers = q['grampers']
    conj = verb.conj(tense,grampers)
    if answer_text == r['correct']:
      q['code'] = 0
    else:
      q['code'] = 2
    q['']
    return q

def evaluate__verbs_simple_present_sense_conjugation(q):
  #v = Verb(
  return q

